package main.java.com.dao.impl;

import main.java.com.config.BaseDao;
import main.java.com.dao.CategoryDAO;
import main.java.com.entity.Category;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDAOImpl extends BaseDao implements CategoryDAO {
    //获取所有类别信息
    @Override
    public List<Category> getAllCategories() {
        List<Category> categories = new ArrayList<>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            String sql = "SELECT * FROM category";
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                Category category = new Category();
                category.setId(resultSet.getLong("id"));
                category.setType(resultSet.getInt("type"));
                category.setName(resultSet.getString("name"));
                category.setSort(resultSet.getInt("sort"));
                category.setCreate_time(resultSet.getDate("create_time"));
                category.setUpdate_time(resultSet.getDate("update_time"));
                category.setCreate_user(resultSet.getLong("create_user"));
                category.setUpdate_user(resultSet.getLong("update_user"));
                categories.add(category);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(connection, preparedStatement, resultSet);
        }

        return categories;
    }

    //根据类别ID获取类别信息
    @Override
    public Category getCategoryById(Long id) {
        Category category = null;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            String sql = "SELECT * FROM category WHERE id =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setLong(1, id);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                category = new Category();
                category.setId(resultSet.getLong("id"));
                category.setType(resultSet.getInt("type"));
                category.setName(resultSet.getString("name"));
                category.setSort(resultSet.getInt("sort"));
                category.setCreate_time(resultSet.getDate("create_time"));
                category.setUpdate_time(resultSet.getDate("update_time"));
                category.setCreate_user(resultSet.getLong("create_user"));
                category.setUpdate_user(resultSet.getLong("update_user"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(connection, preparedStatement, resultSet);
        }

        return category;
    }




    private void closeResources(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if (resultSet!= null) {
                resultSet.close();
            }
            if (preparedStatement!= null) {
                preparedStatement.close();
            }
            if (connection!= null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
